package taikai;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.SystemColor;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

class TeamListCellRenderer implements ListCellRenderer {
    private final JPanel panel = new JPanel();
    private final JLabel teamName = new JLabel();
    private final JLabel teamMembers = new JLabel();

    public TeamListCellRenderer() {
        panel.setLayout(new BorderLayout());
        panel.add(teamName, BorderLayout.CENTER);
        panel.add(teamMembers, BorderLayout.SOUTH);
        teamMembers.setForeground(SystemColor.textInactiveText);
        teamMembers.setFont(teamMembers.getFont().deriveFont(10f));
    }

    @Override
    public Component getListCellRendererComponent(JList jlist, Object o, int i, boolean isSelected, boolean bln1) {
        final Team team = (Team) o;
        final StringBuilder buf = new StringBuilder();

        for (Person person : team) {
            if(buf.length()>0) {
                buf.append(", ");
            }
            buf.append(person.getLastName());
        }

        teamName.setText(team.getName());
        teamMembers.setText(buf.toString());

        if(isSelected) {
            panel.setBackground(SystemColor.controlHighlight);
            teamName.setForeground(SystemColor.controlLtHighlight);
        } else {
            panel.setBackground(SystemColor.text);
            teamName.setForeground(SystemColor.textText);
        }

        return panel;
    }
}
